package com.winning.sx.microframework.controller.sysmanage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.util.Tools;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.base_entity.BaseConstService;
import com.winning.sx.microframework.service.entity.EBaseConstService;
import com.winning.sx.microframework.service.entity.EBaseConstTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/manage1/pm-const")
public class ConstController {
    @Autowired
    EBaseConstService service;
    @Autowired
    EBaseConstTypeService eBaseConstTypeService;
    @Autowired
    BaseConstService constService;
    @RequestMapping(value = {"/", ""})
    public ModelAndView init() throws IOException , CustomException {
        ModelAndView mv=new ModelAndView("/manage/pm-const");
        Map param=new HashMap();
        List constType=eBaseConstTypeService.queryList(param).getDataRows();
        mv.addObject("constType",constType);
        List state= constService.queryDropDownList("200");
        mv.addObject("state",state);
        return mv;
    }
    @ResponseBody
    @RequestMapping(value = {"save"})
    public AjaxDataResponseMessage save(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();

        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                service.save(map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }
    @ResponseBody
    @RequestMapping(value = {"delete"})
    public AjaxDataResponseMessage delete(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();

        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                service.delete(map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryList"})
    public AjaxTableResponseMessage queryList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            DataTable all = service.queryList(param);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
}
